package f.f0.g;

import c.j.a.c.y.a.i;
import com.huawei.agconnect.exception.AGCServerException;
import f.a;
import f.a0;
import f.d;
import f.d0;
import f.e;
import f.f0.c;
import f.f0.f.g;
import f.m;
import f.r;
import f.s;
import f.v;
import f.y;
import f.z;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.util.Objects;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;

/* compiled from: RetryAndFollowUpInterceptor */
public final class h implements s {
    public final v a;
    public volatile g b;
    public Object c;
    public volatile boolean d;

    public h(v vVar, boolean z) {
        this.a = vVar;
    }

    public final a a(r rVar) {
        e eVar;
        SSLSocketFactory sSLSocketFactory;
        HostnameVerifier hostnameVerifier;
        r rVar2 = rVar;
        if (rVar2.a.equals("https")) {
            v vVar = this.a;
            SSLSocketFactory sSLSocketFactory2 = vVar.l;
            HostnameVerifier hostnameVerifier2 = vVar.n;
            eVar = vVar.o;
            sSLSocketFactory = sSLSocketFactory2;
            hostnameVerifier = hostnameVerifier2;
        } else {
            sSLSocketFactory = null;
            hostnameVerifier = sSLSocketFactory;
            eVar = hostnameVerifier;
        }
        String str = rVar2.d;
        int i = rVar2.e;
        v vVar2 = this.a;
        return new a(str, i, vVar2.s, vVar2.k, sSLSocketFactory, hostnameVerifier, eVar, vVar2.p, vVar2.b, vVar2.c, vVar2.d, vVar2.h);
    }

    public final y b(a0 a0Var, d0 d0Var) throws IOException {
        if (a0Var != null) {
            int i = a0Var.c;
            String str = a0Var.a.b;
            String str2 = "GET";
            z zVar = null;
            if (i == 307 || i == 308) {
                if (!(str.equals(str2) || str.equals("HEAD"))) {
                    return null;
                }
            } else if (i == AGCServerException.TOKEN_INVALID) {
                Objects.requireNonNull(this.a.q);
                return null;
            } else if (i == AGCServerException.SERVER_NOT_AVAILABLE) {
                a0 a0Var2 = a0Var.j;
                if ((a0Var2 == null || a0Var2.c != AGCServerException.SERVER_NOT_AVAILABLE) && d(a0Var, Integer.MAX_VALUE) == 0) {
                    return a0Var.a;
                }
                return null;
            } else if (i == 407) {
                Proxy proxy;
                if (d0Var != null) {
                    proxy = d0Var.b;
                } else {
                    proxy = this.a.b;
                }
                if (proxy.type() == Type.HTTP) {
                    Objects.requireNonNull(this.a.p);
                    return null;
                }
                throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
            } else if (i != 408) {
                switch (i) {
                    case 300:
                    case 301:
                    case 302:
                    case 303:
                        break;
                    default:
                        return null;
                }
            } else if (!this.a.v) {
                return null;
            } else {
                a0 a0Var3 = a0Var.j;
                if ((a0Var3 == null || a0Var3.c != 408) && d(a0Var, 0) <= 0) {
                    return a0Var.a;
                }
                return null;
            }
            if (!this.a.u) {
                return null;
            }
            String c = a0Var.f.c("Location");
            if (c == null) {
                c = null;
            }
            if (c == null) {
                return null;
            }
            r.a aVar;
            r rVar = a0Var.a.a;
            Objects.requireNonNull(rVar);
            try {
                aVar = new r.a();
                aVar.d(rVar, c);
            } catch (IllegalArgumentException unused) {
                aVar = null;
            }
            r b = aVar != null ? aVar.b() : null;
            if (b == null) {
                return null;
            }
            if (!b.a.equals(a0Var.a.a.a) && !this.a.t) {
                return null;
            }
            y yVar = a0Var.a;
            Objects.requireNonNull(yVar);
            y.a aVar2 = new y.a(yVar);
            if (i.E(str)) {
                String str3 = "PROPFIND";
                boolean equals = str.equals(str3);
                if ((str.equals(str3) ^ 1) != 0) {
                    aVar2.d(str2, null);
                } else {
                    if (equals) {
                        zVar = a0Var.a.d;
                    }
                    aVar2.d(str, zVar);
                }
                if (!equals) {
                    aVar2.c.b("Transfer-Encoding");
                    aVar2.c.b("Content-Length");
                    aVar2.c.b("Content-Type");
                }
            }
            if (!e(a0Var, b)) {
                aVar2.c.b("Authorization");
            }
            aVar2.f(b);
            return aVar2.b();
        }
        throw new IllegalStateException();
    }

    /* JADX WARNING: Removed duplicated region for block: B:32:0x0050 A:{RETURN} */
    /* JADX WARNING: Removed duplicated region for block: B:31:0x004f A:{RETURN} */
    public final boolean c(java.io.IOException r3, f.f0.f.g r4, boolean r5, f.y r6) {
        /*
        r2 = this;
        r4.h(r3);
        r6 = r2.a;
        r6 = r6.v;
        r0 = 0;
        if (r6 != 0) goto L_0x000b;
    L_0x000a:
        return r0;
    L_0x000b:
        r6 = r3 instanceof java.net.ProtocolException;
        r1 = 1;
        if (r6 == 0) goto L_0x0011;
    L_0x0010:
        goto L_0x002d;
    L_0x0011:
        r6 = r3 instanceof java.io.InterruptedIOException;
        if (r6 == 0) goto L_0x001c;
    L_0x0015:
        r3 = r3 instanceof java.net.SocketTimeoutException;
        if (r3 == 0) goto L_0x002d;
    L_0x0019:
        if (r5 != 0) goto L_0x002d;
    L_0x001b:
        goto L_0x002f;
    L_0x001c:
        r5 = r3 instanceof javax.net.ssl.SSLHandshakeException;
        if (r5 == 0) goto L_0x0029;
    L_0x0020:
        r5 = r3.getCause();
        r5 = r5 instanceof java.security.cert.CertificateException;
        if (r5 == 0) goto L_0x0029;
    L_0x0028:
        goto L_0x002d;
    L_0x0029:
        r3 = r3 instanceof javax.net.ssl.SSLPeerUnverifiedException;
        if (r3 == 0) goto L_0x002f;
    L_0x002d:
        r3 = 0;
        goto L_0x0030;
    L_0x002f:
        r3 = 1;
    L_0x0030:
        if (r3 != 0) goto L_0x0033;
    L_0x0032:
        return r0;
    L_0x0033:
        r3 = r4.c;
        if (r3 != 0) goto L_0x004c;
    L_0x0037:
        r3 = r4.b;
        if (r3 == 0) goto L_0x0041;
    L_0x003b:
        r3 = r3.a();
        if (r3 != 0) goto L_0x004c;
    L_0x0041:
        r3 = r4.h;
        r3 = r3.b();
        if (r3 == 0) goto L_0x004a;
    L_0x0049:
        goto L_0x004c;
    L_0x004a:
        r3 = 0;
        goto L_0x004d;
    L_0x004c:
        r3 = 1;
    L_0x004d:
        if (r3 != 0) goto L_0x0050;
    L_0x004f:
        return r0;
    L_0x0050:
        return r1;
        */
        throw new UnsupportedOperationException("Method not decompiled: f.f0.g.h.c(java.io.IOException, f.f0.f.g, boolean, f.y):boolean");
    }

    public final int d(a0 a0Var, int i) {
        String c = a0Var.f.c("Retry-After");
        if (c == null) {
            c = null;
        }
        if (c == null) {
            return i;
        }
        return c.matches("\\d+") ? Integer.valueOf(c).intValue() : Integer.MAX_VALUE;
    }

    public final boolean e(a0 a0Var, r rVar) {
        r rVar2 = a0Var.a.a;
        return rVar2.d.equals(rVar.d) && rVar2.e == rVar.e && rVar2.a.equals(rVar.a);
    }

    public a0 intercept(s.a aVar) throws IOException {
        f fVar = (f) aVar;
        y yVar = fVar.f;
        d dVar = fVar.g;
        m mVar = fVar.h;
        g gVar = new g(this.a.r, a(yVar.a), dVar, mVar, this.c);
        this.b = gVar;
        int i = 0;
        a0 a0Var = null;
        while (!this.d) {
            try {
                a0 b;
                yVar = fVar.b(yVar, r9, null, null);
                if (a0Var != null) {
                    a0.a aVar2 = new a0.a(yVar);
                    a0.a aVar3 = new a0.a(a0Var);
                    aVar3.g = null;
                    b = aVar3.b();
                    if (b.g == null) {
                        aVar2.j = b;
                        b = aVar2.b();
                    } else {
                        throw new IllegalArgumentException("priorResponse.body != null");
                    }
                }
                try {
                    y b2 = b(b, r9.c);
                    if (b2 == null) {
                        r9.g();
                        return b;
                    }
                    c.f(b.g);
                    int i2 = i + 1;
                    if (i2 <= 20) {
                        if (e(b, b2.a)) {
                            c cVar;
                            synchronized (r9.d) {
                                cVar = r9.n;
                            }
                            if (cVar != null) {
                                StringBuilder stringBuilder = new StringBuilder();
                                stringBuilder.append("Closing the body of ");
                                stringBuilder.append(b);
                                stringBuilder.append(" didn't close its backing stream. Bad interceptor?");
                                throw new IllegalStateException(stringBuilder.toString());
                            }
                        }
                        r9.g();
                        gVar = new g(this.a.r, a(b2.a), dVar, mVar, this.c);
                        this.b = gVar;
                        a0Var = b;
                        yVar = b2;
                        i = i2;
                    } else {
                        r9.g();
                        throw new ProtocolException(c.b.a.a.a.A("Too many follow-up requests: ", i2));
                    }
                } catch (IOException e) {
                    r9.g();
                    throw e;
                }
            } catch (f.f0.f.e e2) {
                if (!c(e2.b, r9, false, yVar)) {
                    throw e2.a;
                }
            } catch (IOException e3) {
                if (!c(e3, r9, !(e3 instanceof f.f0.i.a), yVar)) {
                    throw e3;
                }
            } catch (Throwable th) {
                r9.h(null);
                r9.g();
            }
        }
        r9.g();
        throw new IOException("Canceled");
    }
}
